﻿package GUI 
{
	import flash.display.MovieClip;
	import flash.display.MovieClip;
	import com.as3toolkit.events.KeyboarderEvent;
	import com.as3toolkit.ui.Keyboarder;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import caurina.transitions.Tweener;
	import code.*;
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	/**
	 * ...
	 * @author Sam Morrison
	 */
	public class Select extends MovieClip 
	{
		// Player 1
		public var p1_select_mc:MovieClip;
		private var p1Move:Number;
		private var p1SideMove:Number;
		private var p1DeckMove:Number;
		private var p1Active:String = "main";
		private var player1:Player;
		private var p1DeckLayer:Sprite;
		private var p1InfoLayer:Sprite;
		private var p1SelectedCard:int = 0;
		private var p1SelectedSideCard:int;
		private var p1Stored:Array = new Array();
		private var p1SideDeck:Array = new Array();
		private var p1DeckMC:Array = new Array();
		private var p1Deck:Array = new Array();
		private var p1SideDeckBitMaps:Array = new Array();
		
		// Player 2
		public var p2_select_mc:MovieClip;
		private var p2Move:Number;
		private var p2SideMove:Number;
		private var p2DeckMove:Number;
		private var p2Active:String = "main";
		private var player2:Player;
		private var p2DeckLayer:Sprite;
		private var p2InfoLayer:Sprite;
		private var p2SelectedCard:int = 0;
		private var p2SelectedSideCard:int;
		private var p2Stored:Array = new Array();
		private var p2SideDeck:Array = new Array();
		private var p2DeckMC:Array = new Array();
		private var p2Deck:Array = new Array();
		private var p2SideDeckBitMaps:Array = new Array();
		
		// Sound
		public var channel2:SoundChannel = new SoundChannel();
		public var externalSound:Sound;
		
		public function Select(player1:Player, player2:Player) {
			Keyboarder.Instance.addEventListener(KeyboarderEvent.FIRST_PRESS, pressed);
			p1Move = p1SideMove = p1_select_mc.y;
			p2Move = p2SideMove = p2_select_mc.y;
			p1DeckMove = 0;
			p2DeckMove = 0;
			this.player1 = player1;
			this.player2 = player2;
			p1DeckLayer = new Sprite();
			p2DeckLayer = new Sprite();
			p1InfoLayer = new Sprite();
			p2InfoLayer = new Sprite();
			addChildAt(p1DeckLayer,1);
			addChildAt(p2DeckLayer,1);
			addChild(p1InfoLayer);
			addChild(p2InfoLayer);
			loadData();
			
			
			// emptys layer
			if(p1InfoLayer.numChildren != 0)
			{
				var k:int = p1InfoLayer.numChildren;
				while( k -- )
				{
					p1InfoLayer.removeChildAt( k );
				}
			}
			
			var cost:int = (p1Deck[p1SelectedCard] as Card).ManaCost;
			var acc:Number = (p1Deck[p1SelectedCard] as Card).Accuracy;
			var eff:String = (p1Deck[p1SelectedCard] as Card).Effect;
			var mod:Number = (p1Deck[p1SelectedCard] as Card).Modifier;
		
			var name:String = (p1Deck[p1SelectedCard] as Card).CardName;
			var desc:String = (p1Deck[p1SelectedCard] as Card).Description;
		
			var temp:CardDisplay = new CardDisplay();
			var temp1:CardDisplay2 = new CardDisplay2();
			p1InfoLayer.addChild(temp);
			p1InfoLayer.addChild(temp1);
			//p1DeckMC.push(temp);
			temp.y = 650;
			temp.x = 220;
			temp1.y = 570;
			temp1.x = 150;
			temp.cost_txt.text = ""+cost;
			temp.acc_txt.text = ""+acc * 100 + "%";
			temp.eff_txt.text = ""+eff;
			temp.mod_txt.text = ""+mod;
			temp1.name_txt.text = name;
			temp1.desc_txt.text = desc;
			
			
			// emptys layer
			if(p2InfoLayer.numChildren != 0)
			{
				var k:int = p2InfoLayer.numChildren;
				while( k -- )
				{
					p2InfoLayer.removeChildAt( k );
				}
			}
			
			var cost:int = (p2Deck[p2SelectedCard] as Card).ManaCost;
			var acc:Number = (p2Deck[p2SelectedCard] as Card).Accuracy;
			var eff:String = (p2Deck[p2SelectedCard] as Card).Effect;
			var mod:Number = (p2Deck[p2SelectedCard] as Card).Modifier;
			
			var name:String = (p2Deck[p2SelectedCard] as Card).CardName;
			var desc:String = (p2Deck[p2SelectedCard] as Card).Description;
			
			var temp:CardDisplay = new CardDisplay();
			var temp2:CardDisplay2 = new CardDisplay2();
			p2InfoLayer.addChild(temp);
			p2InfoLayer.addChild(temp2);
			//p2DeckMC.push(temp);
			temp.y = 650;
			temp.x = 680;
			temp2.y = 570;
			temp2.x = 610;
			temp.cost_txt.text = ""+cost;
			temp.acc_txt.text = ""+acc * 100 + "%";
			temp.eff_txt.text = ""+eff;
			temp.mod_txt.text = ""+mod;
			
			temp2.name_txt.text = name;
			temp2.desc_txt.text = desc;
			playMusic();
		}
		
		
		// starts sound 
		private function playMusic():void
		{
			var req:URLRequest;
			req = new URLRequest("Ultimate Nerd Music/Final Menu Song (hopefully).mp3");		
    		externalSound = new Sound(req);
    		channel2 = externalSound.play();
    		channel2.addEventListener(Event.SOUND_COMPLETE, loopMusic);
		}
		
		// when background sound is done loop it
		private function loopMusic(e:Event):void
		{
    		if (channel2 != null)
    		{
        		channel2.removeEventListener(Event.SOUND_COMPLETE, loopMusic);
        		playMusic();
    		}
		}
		
		private function loadData(){
			
			for each( var tempCard:Card in player1.TotalDeck){
				if(tempCard.Type != "item")
				p1Deck.push(tempCard);
			}
			
			for(var i:int = 0; i < p1Deck.length; i++){
				var cost:int = (p1Deck[i] as Card).ManaCost;
				var acc:Number = (p1Deck[i] as Card).Accuracy;
				var eff:String = (p1Deck[i] as Card).Effect;
				var mod:Number = (p1Deck[i] as Card).Modifier;
				
				var temp:CardDisplay = new CardDisplay();
				p1DeckLayer.addChild(temp);
				p1DeckMC.push(temp);
				temp.color_mc.gotoAndStop((p1Deck[i] as Card).Type);
				temp.y = i * 50 + 55;
				temp.x = 220;
				temp.cost_txt.text = ""+cost;
				temp.acc_txt.text = ""+acc * 100 + "%";
				temp.eff_txt.text = ""+eff;
				temp.mod_txt.text = ""+mod;
			}
			
			
			for each(var tempCard1:Card in player2.TotalDeck){
				
				if(tempCard1.Type != "item")
				p2Deck.push(tempCard1);
			}
			
			for(var i:int = 0; i < p2Deck.length; i++){
				var cost:int = (p2Deck[i] as Card).ManaCost;
				var acc:Number = (p2Deck[i] as Card).Accuracy;
				var eff:String = (p2Deck[i] as Card).Effect;
				var mod:Number = (p2Deck[i] as Card).Modifier;
				
				var temp:CardDisplay = new CardDisplay();
				temp.color_mc.gotoAndStop((p2Deck[i] as Card).Type);
				p2DeckLayer.addChild(temp);
				p2DeckMC.push(temp);
				temp.y = i * 50 + 55;
				temp.x = 680;
				temp.cost_txt.text = ""+cost;
				temp.acc_txt.text = ""+acc * 100 + "%";
				temp.eff_txt.text = ""+eff;
				temp.mod_txt.text = ""+mod;
			}
		}
		
		private function pressed(e:KeyboarderEvent):void {
			if(e.keyCode == 66 && p1SideDeck.length == 3 && p2SideDeck.length == 3){
				///////////////////////////////////////////////////////////////////////////////////////////////
				//GAME
				///////////////////////////////////////////////////////////////////////////////////////////////
				player1.StoredCards = p1SideDeck;
				player2.StoredCards = p2SideDeck;
				
				channel2.stop();
				channel2.removeEventListener(Event.SOUND_COMPLETE, loopMusic);
				(parent as code.Field).gameInit2();
				
			}
			
			
			
			if (p1Active == "main"){
				if(e.keyCode == 83 && p1Move == 445 && p1SelectedCard < p1Deck.length - 1){
					p1DeckMove -= 50;
					Tweener.addTween(p1DeckLayer, { y: p1DeckMove, alpha:1, time:.2, transition:"easeOutQuint" } );
					p1SelectedCard++;
					
				}
				else if( e.keyCode == 87 && p1Move == 45 && p1SelectedCard > 0){
					p1DeckMove += 50;
					Tweener.addTween(p1DeckLayer, { y: p1DeckMove, alpha:1, time:.2, transition:"easeOutQuint" } );
					p1SelectedCard--;
					
					
					
				}
				else if (e.keyCode == 83 && p1Move < 445){
					p1Move += 50;
					Tweener.addTween(p1_select_mc, { y: p1Move, alpha:1, time:.2, transition:"easeOutQuint" } );
					p1SelectedCard++;
					
					
				}
				else if (e.keyCode == 87 && p1Move > 45) {
					p1Move -= 50;
					Tweener.addTween(p1_select_mc, { y: p1Move, alpha:1, time:.2, transition:"easeOutQuint" } );
					p1SelectedCard--;
				}
				
				
				// emptys layer
					if(p1InfoLayer.numChildren != 0)
					{
						var k:int = p1InfoLayer.numChildren;
						while( k -- )
						{
							p1InfoLayer.removeChildAt( k );
						}
					}
					
					var cost:int = (p1Deck[p1SelectedCard] as Card).ManaCost;
					var acc:Number = (p1Deck[p1SelectedCard] as Card).Accuracy;
					var eff:String = (p1Deck[p1SelectedCard] as Card).Effect;
					var mod:Number = (p1Deck[p1SelectedCard] as Card).Modifier;
				
					var name:String = (p1Deck[p1SelectedCard] as Card).CardName;
					var desc:String = (p1Deck[p1SelectedCard] as Card).Description;
				
					var temp:CardDisplay = new CardDisplay();
					temp.color_mc.gotoAndStop((p1Deck[p1SelectedCard] as Card).Type);
					var temp1:CardDisplay2 = new CardDisplay2();
					p1InfoLayer.addChild(temp);
					p1InfoLayer.addChild(temp1);
					//p1DeckMC.push(temp);
					temp.y = 650;
					temp.x = 220;
					temp1.y = 570;
					temp1.x = 150;
					temp.cost_txt.text = ""+cost;
					temp.acc_txt.text = ""+acc * 100 + "%";
					temp.eff_txt.text = ""+eff;
					temp.mod_txt.text = ""+mod;
				
					temp1.name_txt.text = name;
					temp1.desc_txt.text = desc;
				
				
				
				
				
			}
			
			if (p2Active == "main"){
				if(e.keyCode == 75 && p2Move == 445 && p2SelectedCard < p2Deck.length - 1){
					p2DeckMove -= 50;
					Tweener.addTween(p2DeckLayer, { y: p2DeckMove, alpha:1, time:.2, transition:"easeOutQuint" } );
					p2SelectedCard++;
				}
				else if( e.keyCode == 73 && p2Move == 45 && p2SelectedCard > 0){
					p2DeckMove += 50;
					Tweener.addTween(p2DeckLayer, { y: p2DeckMove, alpha:1, time:.2, transition:"easeOutQuint" } );
					p2SelectedCard--;
				}
				else if (e.keyCode == 75 && p2Move < 445){
					p2Move += 50;
					Tweener.addTween(p2_select_mc, { y: p2Move, alpha:1, time:.2, transition:"easeOutQuint" } );
					p2SelectedCard++;
				}
				else if (e.keyCode == 73 && p2Move > 45 ) {
					p2Move -= 50;
					Tweener.addTween(p2_select_mc, { y: p2Move, alpha:1, time:.2, transition:"easeOutQuint" } );
					p2SelectedCard--;
				}
				
				// emptys layer
					if(p2InfoLayer.numChildren != 0)
					{
						var k:int = p2InfoLayer.numChildren;
						while( k -- )
						{
							p2InfoLayer.removeChildAt( k );
						}
					}
					
				var cost:int = (p2Deck[p2SelectedCard] as Card).ManaCost;
				var acc:Number = (p2Deck[p2SelectedCard] as Card).Accuracy;
				var eff:String = (p2Deck[p2SelectedCard] as Card).Effect;
				var mod:Number = (p2Deck[p2SelectedCard] as Card).Modifier;
				
				var name:String = (p2Deck[p2SelectedCard] as Card).CardName;
				var desc:String = (p2Deck[p2SelectedCard] as Card).Description;
				
				var temp:CardDisplay = new CardDisplay();
				var temp2:CardDisplay2 = new CardDisplay2();
				temp.color_mc.gotoAndStop((p2Deck[p2SelectedCard] as Card).Type);
				p2InfoLayer.addChild(temp);
				p2InfoLayer.addChild(temp2);
				//p2DeckMC.push(temp);
				temp.y = 650;
				temp.x = 680;
				temp2.y = 570;
				temp2.x = 610;
				temp.cost_txt.text = ""+cost;
				temp.acc_txt.text = ""+acc * 100 + "%";
				temp.eff_txt.text = ""+eff;
				temp.mod_txt.text = ""+mod;
				
				temp2.name_txt.text = name;
				temp2.desc_txt.text = desc;
			}
			
			
			
			
			
			//@author Matthew DeNovio
			
			if(e.keyCode == 81 && p1Active == "main"){
				if(p1DeckMC[p1SelectedCard].currentFrame == 1){
					var bool:Boolean;
					for(var i:int = 0; i < p1SideDeck.length; i++)
					{	
						if(p1SideDeck[i].Type == p1Deck[p1SelectedCard].Type)
						{
							// remove card from select if it exists there
							if(contains(p1SideDeckBitMaps[i]))
							   	removeChild(p1SideDeckBitMaps[i]);
							// change thepreviously selected to appear unselected
							p1DeckMC[p1Stored[i]].gotoAndStop(1);
							// replace the newly selected in the arrays
							p1SideDeck[i] = p1Deck[p1SelectedCard];
							p1Stored[i] = p1SelectedCard;
							//Change display, Position Image on screen and add it
							p1DeckMC[p1SelectedCard].gotoAndStop(2);
							p1SideDeckBitMaps[i] = 	p1Deck[p1SelectedCard].CardImage
							p1SideDeckBitMaps[i].x = 35
							p1SideDeckBitMaps[i].y = 50 + i * 150;
								addChild(p1SideDeckBitMaps[i]);
							bool = true;
							break;
						}				
						bool = false;						
					}
					
					// if there isnt already a card of this type in the array
					if(!bool)
					{
						p1DeckMC[p1SelectedCard].gotoAndStop(2);
						p1Stored.push(p1SelectedCard);
						p1SideDeck.push(p1Deck[p1SelectedCard]);
						p1SideDeckBitMaps.push(p1Deck[p1SelectedCard].CardImage);
						var dbmLength = p1SideDeckBitMaps.length - 1;
						p1SideDeckBitMaps[dbmLength].x =35
						p1SideDeckBitMaps[dbmLength].y = 50 + dbmLength * 150
						addChild(p1SideDeckBitMaps[dbmLength]);
					}
				}
				/*
				else{
					p1DeckMC[p1SelectedCard].gotoAndStop(1);
				}
				*/
			}
			
			
			
			
			// Click on card
			if(e.keyCode == 85 && p2Active == "main"){
				if(p2DeckMC[p2SelectedCard].currentFrame == 1){
					var bool:Boolean;
					for(var i:int = 0; i < p2SideDeck.length; i++)
					{	
						if(p2SideDeck[i].Type == p2Deck[p2SelectedCard].Type)
						{
							if(contains(p2SideDeckBitMaps[i]))
							   	removeChild(p2SideDeckBitMaps[i]);
							p2DeckMC[p2Stored[i]].gotoAndStop(1);
							p2SideDeck[i] = p2Deck[p2SelectedCard];
							p2Stored[i] = p2SelectedCard;
							p2DeckMC[p2SelectedCard].gotoAndStop(2);
							p2SideDeckBitMaps[i] = 	p2Deck[p2SelectedCard].CardImage
							p2SideDeckBitMaps[i].x = stage.stageWidth - 115
							p2SideDeckBitMaps[i].y = 50 + i * 150;
								addChild(p2SideDeckBitMaps[i]);
							bool = true;
							break;
						}				
						bool = false;						
					}
					
					if(!bool)
					{
						p2DeckMC[p2SelectedCard].gotoAndStop(2);
						p2Stored.push(p2SelectedCard);
						p2SideDeck.push(p2Deck[p2SelectedCard]);
						p2SideDeckBitMaps.push(p2Deck[p2SelectedCard].CardImage);
						var dbmLength = p2SideDeckBitMaps.length - 1;
						p2SideDeckBitMaps[dbmLength].x = stage.stageWidth - 115
						p2SideDeckBitMaps[dbmLength].y = 50 + dbmLength * 150
						addChild(p2SideDeckBitMaps[dbmLength]);
					}
				}
			}
		}
		
		public function remove(){
			Keyboarder.Instance.removeEventListener(KeyboarderEvent.FIRST_PRESS, pressed);
		}
	}

}